package com.scaffold.authority.action;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import wangjg.commons.page.Page;

import com.scaffold.BasicBizAction;
import com.scaffold.authority.entity.Authority;
import com.scaffold.authority.service.AuthorityService;

@Controller
public class AuthorityJsonAction extends BasicBizAction{
	@Autowired
	private AuthorityService authorityService;

	@RequestMapping("/authority/list.json")
	public String list(Integer type, String name, String displayName, String keyword){
		Page page = this.getPage(5);
		List<Authority> list = authorityService.pageQuery(page, type, name, displayName, keyword);
		return this.getPageResult(page, list);
	}
	
	@RequestMapping("/authority/save.json")
	public String save(Long id){
		if(id==null){
			Authority authority = new Authority();
			this.bindParams(authority);
			this.setOwner(authority);
			this.authorityService.save(authority);
		}else{
			Authority authority = this.authorityService.getAuthority(id);
			this.checkOwner(authority);
			this.bindParams(authority);
			this.authorityService.update(authority);
		}
		return this.getSuccessResult();
	}

	@RequestMapping("/authority/get.json")
	public String get(Long id){
		Authority authority = authorityService.getAuthority(id);
		this.checkOwner(authority);
		return this.getSuccessResult(authority);
	}

	@RequestMapping("/authority/delete.json")
	public String delete(Long id){
		Authority authority = authorityService.getAuthority(id);
		this.checkOwner(authority);
		this.authorityService.delete(authority);
		return this.getSuccessResult();
	}


	@RequestMapping("/authority/batchDelete.json")
	public String batchDelete(String idseq){
		this.authorityService.batchDelete(idseq);
		return this.getSuccessResult();
	}

	@RequestMapping("/authority/select.json")
	public String select(Integer type, String name, String displayName, String keyword){
		List<Authority> list = authorityService.pageQuery(null, type, name, displayName, keyword);
		return this.getSuccessResult(this.getOptions(list, "name"));
	}


	@RequestMapping("/authority/setting/saveResources.json")
	public String saveResources(Long authorityId, String resourceId) {
		Authority authority = this.authorityService.getAuthority(authorityId);
		this.checkOwner(authority);
		authorityService.saveResources(authorityId, resourceId);
		return this.getSuccessResult();
	}

	@RequestMapping("/authority/setting/getResourceIdList.json")
	public String getResourceIdList(Long authorityId) {
		Authority authority = this.authorityService.getAuthority(authorityId);
		this.checkOwner(authority);
		List<Long> list = authorityService.getResourceIdList(authorityId);
		return this.getSuccessResult(list);
	}

}
